package com.wj.requestMappingConfig;

import java.lang.reflect.Method;
import java.util.Objects;

import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.util.Assert;
import org.springframework.web.servlet.mvc.condition.RequestCondition;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

import com.wj.annotaion.ApiVersion;

/*================================================================
说明：请求匹配处理器

作者          时间             	  注释
温为菁      2021-04-04		  创建
==================================================================*/
public class ApiRequestMappingHandlerMapping extends RequestMappingHandlerMapping{

    @Override
    protected RequestCondition<?> getCustomMethodCondition(Method method) {
        // 扫描方法上的 @ApiVersion
        ApiVersion apiVersion = AnnotationUtils.findAnnotation(method, ApiVersion.class);
        return createRequestCondition(apiVersion);
    }
    
    
    private RequestCondition<ApiVersionCondition> createRequestCondition(ApiVersion apiVersion) {
        if (Objects.isNull(apiVersion)) {
        	return null;
        }
        int[] value = apiVersion.value();
        for (int i : value) {
        	 Assert.isTrue(i >= 1, "接口版本号必须大于或等于1");
		}
       
        return new ApiVersionCondition(value);
    }
}
